package com.ruleengine.admin.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruleengine.admin.vo.CreateRuleEngineResponse;
import com.ruleengine.admin.vo.GetRuleEngineResponse;
import com.ruleengine.db.model.Ruleengine;
import com.ruleengine.db.model.RuleengineStatus;
import com.ruleengine.db.service.RuleengineService;

import cn.hutool.core.lang.UUID;

/**
 * RuleEngineController
 *
 * @author lianghanwei on 2024/3/15
 */
@RestController
@RequestMapping(value = "/v1/ruleengine/bifromq", produces = "application/json;charset=UTF-8")
public class RuleEngineController {

    private final RuleengineService ruleengineService;

    public RuleEngineController(RuleengineService ruleengineService) {
        this.ruleengineService = ruleengineService;
    }

    @PostMapping("/{instanceId}/ruleengine")
    public CreateRuleEngineResponse createRuleEngine(
        @RequestParam String tenantId,
        @PathVariable String instanceId) {

        Ruleengine ruleengine = Ruleengine.builder()
            .tenantId(tenantId)
            .instanceId(instanceId)
            .ruleengineId(UUID.randomUUID().toString())
            .status(RuleengineStatus.ACTIVE)
            .build();

        ruleengineService.save(ruleengine);

        CreateRuleEngineResponse response = new CreateRuleEngineResponse();
        response.setRuleengineId(ruleengine.getRuleengineId());

        return response;
    }

    @DeleteMapping("/{instanceId}/ruleengine")
    public void deleteRuleEngine(
        @RequestParam String tenantId,
        @PathVariable String instanceId,
        @RequestParam String ruleengineId) {

        ruleengineService.softDeleteRuleengine(tenantId, instanceId, ruleengineId);
    }

    @GetMapping("/{instanceId}/ruleengine")
    public GetRuleEngineResponse getRuleEngine(
        @RequestParam String tenantId,
        @PathVariable String instanceId) {

        Ruleengine ruleengine = ruleengineService.getRuleengine(tenantId, instanceId);

        GetRuleEngineResponse response = new GetRuleEngineResponse();
        response.setRuleengineId(ruleengine.getRuleengineId());
        response.setStatus(ruleengine.getStatus());

        return response;
    }

}
